using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace LastFMPPC
{
	public delegate void VoidInvoker();
	
	public partial class FormMain : Form
	{
		private LastFM lastFM = null;

		/// <summary>
		/// 
		/// </summary>
		public FormMain()
		{
			InitializeComponent();
			statusBar.Text = "Disconnected.";
		}

		/// <summary>
		/// Capturing LastFM state changes.
		/// </summary>
		/// <param name="state">LastFM state.</param>
		private void stateCallback(LastFMState state)
		{
			statusBar.Invoke((VoidInvoker)delegate() { if (lastFM != null) statusBar.Text = lastFM.StateToString(lastFM.LastFMState); });
		}

		/// <summary>
		/// Capturing error messages.
		/// </summary>
		/// <param name="message">Error message.</param>
		private void errorCallback(string message)
		{
			MessageBox.Show(message, "Error!");
		}

		private void metadataCallback(Metadata metadata)
		{
			textBox_Artist.Invoke((VoidInvoker)delegate() { textBox_Artist.Text = metadata.Artist; });
			textBox_Album.Invoke((VoidInvoker)delegate() { textBox_Album.Text = metadata.Album; });
			textBox_Track.Invoke((VoidInvoker)delegate() { textBox_Track.Text = metadata.Track; });
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FormMain_Closing(object sender, CancelEventArgs e)
		{
			if (lastFM != null) lastFM.Dispose();
			lastFM = null;
		}

		private void menuItem_About_Click(object sender, EventArgs e)
		{
			FormAbout form = new FormAbout();
			form.ShowDialog();
			form.Close();
		}

		private void timer_Metadata_Tick(object sender, EventArgs e)
		{
			if (lastFM != null && lastFM.LastFMState == LastFMState.Playing)
			{
				lastFM.GetMetadata();
			}
			else
			{
				textBox_Artist.Text = "";
				textBox_Album.Text = "";
				textBox_Track.Text = "";
			}
		}

		private void FormMain_Load(object sender, EventArgs e)
		{
		}

		private void menuItem_Connect_Click(object sender, EventArgs e)
		{
			if (lastFM != null && lastFM.LastFMState != LastFMState.Disconnected) return;

			try
			{
				FormUserInfo form = new FormUserInfo();
				form.ShowDialog();
				if (form.DialogResult == DialogResult.OK)
				{
					lastFM = new LastFM(stateCallback, errorCallback, metadataCallback);
					lastFM.Connect(form.UserName, form.PasswordMD5);
				}
			}
			catch (LastException lexp)
			{
				if (lastFM != null) lastFM.Dispose();
				lastFM = null;
				statusBar.Text = "Disconnected.";
			}
			catch (Exception exp)
			{
				if (lastFM != null) lastFM.Dispose();
				lastFM = null;
				statusBar.Text = "Disconnected.";
			}
		}

		private void menuItem_Disconnect_Click(object sender, EventArgs e)
		{
			if (lastFM == null ||
				(lastFM.LastFMState != LastFMState.Connected &&
				lastFM.LastFMState != LastFMState.Playing))
				return;

			if (lastFM != null) lastFM.Dispose();
			lastFM = null;

			textBox_Album.Text = "";
			textBox_Artist.Text = "";
			textBox_Track.Text = "";
		}

		private void menuItem_ChangeChannel_Click(object sender, EventArgs e)
		{
			if (lastFM == null ||
				(lastFM.LastFMState != LastFMState.Connected &&
				lastFM.LastFMState != LastFMState.Playing &&
				lastFM.LastFMState != LastFMState.Buffering))
				return;


		}

		private void trackBar_Volume_ValueChanged(object sender, EventArgs e)
		{
			if (lastFM == null || lastFM.LastFMState != LastFMState.Playing) return;

			lastFM.SetVolume(trackBar_Volume.Value);
		}

		private void button_Skip_Click(object sender, EventArgs e)
		{
			if (lastFM == null || lastFM.LastFMState != LastFMState.Playing) return;

			lastFM.Command("skip");
		}

		private void timer_WatchDog_Tick(object sender, EventArgs e)
		{
			if (lastFM != null)
			{
				if (lastFM.LastFMState == LastFMState.Playing)
				{
					// preventing from suspend.
					Helper.IdleTimerReset();
				}
				if (lastFM.LastFMState == LastFMState.Error)
				{
					lastFM.Dispose();
					lastFM = null;
					//errorCallback("Error during playback");
					statusBar.Text = "Disconnected.";
				}
			}
		}
	}
}